#include "head.h"
#include "../include/user.h"

user_t client[10] =
    {
        {"张三", "123", 0, 789},
        {"李四", "345", 1, 891},
        {"王五", "312", 2, 213},
        {"赵六", "679", 3, 783}};

user_t user = {"墨阳", "1126", 3, 9999};

void Log_In()
{
    u_list *head = init_u_list();
    head->next = NULL;

    u_node *L2 = (u_node *)malloc(sizeof(u_node));
    L2->next = NULL;

    if (head == NULL)
    {
        ERRLOG("创建失败");
        return;
    }
    for (int i = 0; i < 4; i++)
    {
        u_node *L1 = (u_node *)malloc(sizeof(u_node));
        L1->next = NULL;
        L1->userm = client[i];
        if (head->next == NULL)
        {
            head->next = L1;
            L1->next = NULL;
        }
        else
        {
            L2->next = L1;
        }
        L2 = L1;
        //   printf("用户名:%s  密码:%s  信息等级:%d  余额:%d\n",L1->userm->name,L1->userm->passwd,L1->userm->IDcard,L1->userm->value);
        // free(L1);
    }
    // 插入
    insert_node(head, user, 2);
    // 遍历
    print_u_list(head);
    insert_file_u_node(head);
}

void insert_node(u_list *list, user_t data, int pos)
{
    if (list == NULL)
    {
        ERRLOG("传递来的链表为空");
        return;
    }
    u_node *L1 = (u_node *)malloc(sizeof(u_node));
    L1->userm = data;

    if (pos == 1)
    {
        L1->next = list->next;
        list->next = L1;
    }
    else
    {
        u_node *L2 = (u_node *)malloc(sizeof(u_node));
        L2 = list->next;

        for (size_t i = 1; i < pos - 1; i++)
        {
            L2 = L2->next;
        }

        L1->next = L2->next;

        L2->next = L1;
    }
}

void print_u_list(u_list *list)
{
    if (list == NULL)
    {
        ERRLOG("传递来的链表为空");
        return;
    }
    u_node *L1 = list->next;
    while (L1 != NULL)
    {
        printf("用户名:%s  密码:%s  信息等级:%d  余额:%d\n", L1->userm.name, L1->userm.passwd, L1->userm.IDcard, L1->userm.value);
        L1 = L1->next;
    }
}

// 创建
u_list *init_u_list()
{
    u_list *L1 = (u_list *)malloc(sizeof(u_list));
    if (L1 == NULL)
    {
        ERRLOG("头指针创建失败");
        return NULL;
    }
    L1->next = NULL;
    return L1;
}

// 用户写入文件
void insert_file_u_node(u_list *list)
{
    if (list == NULL)
    {
        ERRLOG("传递来的链表为空");
        return;
    }
    FILE *fp0 = fopen("/home/student/桌面/代码/08/project/data/regular_users.txt", "w");
    FILE *fp1 = fopen("/home/student/桌面/代码/08/project/data/silver_members.txt", "w");
    FILE *fp2 = fopen("/home/student/桌面/代码/08/project/data/gold_members.txt", "w");
    FILE *fp3 = fopen("/home/student/桌面/代码/08/project/data/platinum_members.txt", "w");
    u_node *L1 = list->next;  
    while (L1 != NULL)
    {
        switch (L1->userm.IDcard)
        {
        case 0:
            fprintf(fp0, "%s\t%s\t%d\t%d\n", L1->userm.name, L1->userm.passwd, L1->userm.IDcard, L1->userm.value);
            break;
        case 1:
            fprintf(fp1, "%s\t%s\t%d\t%d\n", L1->userm.name, L1->userm.passwd, L1->userm.IDcard, L1->userm.value);
            break;
        case 2:
            fprintf(fp2, "%s\t%s\t%d\t%d\n", L1->userm.name, L1->userm.passwd, L1->userm.IDcard, L1->userm.value);
            break;
        case 3:
            fprintf(fp3, "%s\t%s\t%d\t%d\n", L1->userm.name, L1->userm.passwd, L1->userm.IDcard, L1->userm.value);
            break;
        }
        L1 = L1->next;
    }
    fclose(fp0);
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);
}